<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class MigrationCompaniesData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migration:companies';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Migration origin database companies data to new databases';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
		DB::connection('mozhu')->table('companies')->orderBy('id')->chunk(10, function ($companies) {
			$companiesAttributes = [];
			foreach ($companies as $company) {
				$companiesAttributes[] = [
					'title' => $company->company_name,
					'legal' => $company->representative,
					'city' => $company->city,
					'address' => $company->location,
					'telephone' => $company->telephone,
					'email' => $company->email,
					'certificate' => $company->license_pic,
					'introduction' => $company->intro,
					'status' => $company->status,
					'creator_id' => $company->user_id,
					'created_at' => $company->created_at,
					'updated_at' => $company->updated_at,
					'deleted_at' => $company->deleted_at,
				];
			}
			DB::table('companies')->insert($companiesAttributes);
		});
		$this->info('Success migration');
    }
}
